﻿// -----------------------------------------------------------------------
// <copyright file="GetUserByIdAsyncQuery.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Queries.Users
{
	using System.Data.Entity;
	using System.Threading.Tasks;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Database.Dto;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Mappers;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.Model.Queries.Users;
	using Moveax.DataAccess.Cqrs.Highway.UnitTests.DataAccess.Queries;
	using Moveax.DataAccess.Cqrs.Highway.UnitTests.Model;

	internal class GetUserByIdAsyncQuery : QueryBaseClass, IGetUserByIdAsyncQuery
	{
		public GetUserByIdAsyncQuery(IDataSource dataSource)
			: base(dataSource)
		{
		}

		public async Task<User> Execute(int userId, bool includeLinks)
		{
			var user = includeLinks ?
				await this._dataSource.Set<UserDto>(p => p.Links)
					.FirstOrDefaultAsync(p => p.Id == userId)
					.ConfigureAwait(false) :
				await this._dataSource.Set<UserDto>()
					.FirstOrDefaultAsync(p => p.Id == userId)
					.ConfigureAwait(false);

			return user.ToModel();
		}
	}
}